// pages/address/address.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: [],
    where: '',
    have: true,
  },
  // 获取收货地址列表
  getaddresslist() {
    var that = this;
    wh.Post('/api/shop.User_address/user_address_list', {}, function(res) {
      console.log('收货地址列表', res)
      that.setData({
        address: res
      })
      if (that.data.address == 0) {
        that.setData({
          have: false,
        });
      } else {
        that.setData({
          have: true,
        });
      }
    });
  },
  // 设为默认
  addDefault: function(e) {
    console.log(e)
    let that = this;
    // 选中
    let id = e.currentTarget.dataset.id,
      list = this.data.address,
      inx = e.currentTarget.dataset.inx;
    for (let i in list) {
      list[i].is_default = 0;
    }
    let is_default = list[inx].is_default;
    list[inx].is_default = !is_default;
    that.setData({
      address: list
    });
    wh.Post('/api/shop.User_address/set_default', {
      id: id
    }, function(res) {
      wx.showToast({
        title: '设置成功',
        icon: 'none',
      })
    });
  },
  // 编辑
  compile(e) {
    let that = this,
      id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../addAddress/addAddress?id=' + id,
    })
  },
  //删除地址
  deleteAddress: function(e) {
    let that = this,
      id = e.currentTarget.dataset.id,
      list = this.data.address,
      inx = e.currentTarget.dataset.inx;
    wx.showModal({
      title: '',
      content: '是否删除该订单',
      success: function(res) {
        if (res.confirm) {
          wh.Post('/api/shop.User_address/del_user_addr', {
            id: id
          }, function(res) {
            wx.showToast({
              title: '删除成功',
            });
          })
          let arr = that.data.address;
          arr.splice(inx, 1)
          that.setData({
            address: arr
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  },
  // 去订单页
  tosubmit(e) {
    let that = this;
    if (that.data.where == 1) {
      let  id = e.currentTarget.dataset.id;
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];  //上一个页面
      prevPage.setData({
        addresst_id: id
      })
      wx.navigateBack({
        delta: 1
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.where == 1) {
      this.setData({
        where: options.where
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getaddresslist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})